package com.example.xymarket.myView;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

import com.example.xymarket.R;

import java.util.Map;

public class MyDialog  extends Dialog  implements View.OnClickListener {

    private Context context;

    private Map<String,Object> map;

    private int layoutResID=R.layout.my_dialog;//布局文件id

    private final int[] listenedItem={R.id.cancle, R.id.OK,R.id.chose_image};

    public EditText getTitle_input() {
        return title_input;
    }

    public EditText getNum_input() {
        return num_input;
    }

    public EditText getPrice_input() {
        return price_input;
    }

    public void setImageView(Bitmap bitmap)
    {
        imageView.setImageBitmap(bitmap);
    }

    public Spinner getSpinner() {
        return spinner;
    }

    private Spinner spinner;
    private EditText title_input;
    private EditText num_input;
    private EditText price_input;
    private ImageView imageView;

    public MyDialog(Context context,Map<String,Object> map){
        super(context, R.style.Dialog);//加载dialog的样式
        this.context = context;
        this.map=map;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //提前设置Dialog的一些样式
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);//设置dialog显示居中
        setContentView(layoutResID);
        spinner = (Spinner) findViewById(R.id.spinner);
        String[] choice={"新鲜水果","肉禽蛋品","水产海鲜","面点甜食","厨房调料","酒水饮料","日常百货","休闲零食"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
                R.layout.simple_spinner_item,choice);
        //设置下拉样式以后显示的样式
        adapter.setDropDownViewResource(R.layout.my_drop_down_item);
        spinner.setAdapter(adapter);

        WindowManager windowManager = ((Activity)context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = display.getWidth()*4/5;// 设置dialog宽度为屏幕的4/5
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(true);//点击外部Dialog消失
//        遍历控件id添加点击注册
        for(int id:listenedItem){
            findViewById(id).setOnClickListener(this);
        }
        title_input=(EditText) findViewById(R.id.title_input);
        num_input=(EditText) findViewById(R.id.num_input);
        price_input=(EditText) findViewById(R.id.price_input);
        imageView=(ImageView) findViewById(R.id.chose_image);
        spinner=(Spinner)findViewById(R.id.spinner);
        init();
    }

    void init()
    {
        title_input.setText((String)map.get("name"));
        num_input.setText(String.valueOf((Integer) map.get("stock")));
        price_input.setText(String.valueOf((Integer) map.get("price")));
        Bitmap bitmap=(Bitmap) map.get("imageView");
        imageView.setImageBitmap(bitmap);

    }

    private OnCenterItemClickListener listener;

    public interface OnCenterItemClickListener {
        void OnCenterItemClick(MyDialog dialog, View view);
    }
    //很明显我们要在这里面写个接口，然后添加一个方法
    public void setOnCenterItemClickListener(OnCenterItemClickListener listener) {
        this.listener = listener;
    }


    @Override
    public void onClick(View v) {

        listener.OnCenterItemClick(this,v);
    }
}

